<%@ include file="/common/taglibs.jsp"%>

<script type="text/javascript">var removeMessage ="<fmt:message key="bond.remove.products.message"/>";</script>

<head>
    <title><fmt:message key="bondCompanyDetail.title"/></title>
    <meta name="menu" content="BondCompanyMenu"/>
    <meta name="heading" content="<fmt:message key='bondCompanyDetail.heading'/>"/>
</head>

<c:set var="delObject" scope="request"><fmt:message key="bondCompanyList.bondCompany"/></c:set>
<script type="text/javascript">var msgDelConfirm =
   "<fmt:message key="delete.confirm"><fmt:param value="${delObject}"/></fmt:message>";
</script>

<section class="content-header">
    <h1>
        <fmt:message key="bondCompanyDetail.heading"/>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/loanRequests"><i class="fa fa-dashboard"></i><fmt:message key="bondCompanyDetail.heading"/></a></li>
        <li class="active"><fmt:message key="bondCompanyDetail.heading"/></li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title"><fmt:message key="bondCompanyDetail.heading"/></h3>
                </div>
                <div class="box-body">
                    <%@ include file="/common/messages.jsp" %>
                    <div class="row">
                        <div class="col-sm-12">
                            <form:errors path="*" cssClass="alert alert-danger alert-dismissable" element="div"/>
                            <form:form commandName="bondCompany" method="post" action="bondCompanyform" cssClass="well form-horizontal margin"
                                       id="bondCompanyForm" onsubmit="return validateBondCompany(this)">
                                <form:hidden path="id" id="bondCompanyId"/>
                                <form:hidden path="bondUser.id"/>
                                <form:hidden path="bondUser.version"/>
                                <div class="form-group">
                                    <spring:bind path="bondCompany.name">
                                    <appfuse:label key="bondCompany.name" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="name" id="name"  maxlength="255"/>
                                        <form:errors path="name" cssClass="help-block"/>
                                    </div>
                                        <spring:bind path="bondCompany.autoAudit">
                                        <appfuse:label key="bondCompany.autoAudit" styleClass="control-label col-sm-1"/>
                                        <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                            </spring:bind>
                                            <form:checkbox path="autoAudit" id="autoAudit" />
                                            <form:errors path="autoAudit" cssClass="help-block"/>
                                        </div>
                                </div>
                                <div class="form-group">
                                    <spring:bind path="bondCompany.parentId">
                                    <appfuse:label key="bondCompany.parent" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                            <input hidden value="${bondCompany.parent.name}" id="parentValue"/>
                                            <input hidden value="${bondCompany.parent.id}" id="parentValueId"/>
                                            <form:select path="parentId" cssClass="form-control" id="parent">
                                                <option value=""><fmt:message key="option.select"/></option>
                                                <form:options items="${availableProviders}" itemValue="value" itemLabel="label"/>
                                            </form:select>
                                        <form:errors path="parentId" cssClass="help-block"/>
                                    </div>
                                        <spring:bind path="bondCompany.slaveProvider">
                                        <appfuse:label key="bondCompany.slaveProvider" styleClass="control-label col-sm-1"/>
                                        <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                            </spring:bind>
                                            <form:checkbox path="slaveProvider" id="slaveProvider" />
                                            <form:errors path="slaveProvider" cssClass="help-block"/>
                                        </div>
                                        <spring:bind path="bondCompany.masterProvider">
                                        <appfuse:label key="bondCompany.masterProvider" styleClass="control-label col-sm-1"/>
                                        <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                            </spring:bind>
                                            <form:checkbox path="masterProvider" id="masterProvider" />
                                            <form:errors path="masterProvider" cssClass="help-block"/>
                                        </div>
                                </div>
                                <div class="form-group">
                                    <spring:bind path="bondCompany.bondUser.username">
                                    <appfuse:label key="admin.username" path="bondCompany.bondUser.username" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bondUser.username" id="adminUser.username"  maxlength="255"/>
                                        <form:errors path="bondUser.username" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.bondUser.password">
                                    <appfuse:label key="user.password" path="bondCompany.bondUser.password" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:password cssClass="form-control" path="bondUser.password" id="adminUser.password"  maxlength="255" showPassword="true"/>
                                        <form:errors path="bondUser.password" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.bondUser.passwordHint">
                                    <appfuse:label key="user.passwordHint" path="bondCompany.bondUser.passwordHint" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bondUser.passwordHint" id="adminUser.passwordHint"  maxlength="255"/>
                                        <form:errors path="bondUser.passwordHint" cssClass="help-block"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <spring:bind path="bondCompany.bondUser.name">
                                    <appfuse:label key="dealer.contactPerson" path="bondCompany.bondUser.name" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bondUser.name" id="adminUser.name"  maxlength="255"/>
                                        <form:errors path="bondUser.name" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.bondUser.phoneNumber">
                                    <appfuse:label key="dealer.contactNumber" path="bondCompany.bondUser.phoneNumber" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bondUser.phoneNumber" id="adminUser.phoneNumber"  maxlength="255"/>
                                        <form:errors path="bondUser.phoneNumber" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.bondUser.email">
                                    <appfuse:label key="user.email" path="bondCompany.bondUser.email" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bondUser.email" id="adminUser.email"  maxlength="255"/>
                                        <form:errors path="bondUser.email" cssClass="help-block"/>
                                    </div>

                                </div>
                                <div class="form-group">
                                    <spring:bind path="bondCompany.bank">
                                    <appfuse:label key="bondCompany.bank" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bank" id="bank"  maxlength="255"/>
                                        <form:errors path="bank" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.bankAccount">
                                    <appfuse:label key="bondCompany.bankAccount" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="bankAccount" id="bankAccount"  maxlength="255"/>
                                        <form:errors path="bankAccount" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="bondCompany.cardName">
                                    <appfuse:label key="bondCompany.cardName" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:input cssClass="form-control" path="cardName" id="cardName"  maxlength="255"/>
                                        <form:errors path="cardName" cssClass="help-block"/>
                                    </div>
                                </div>
                                <form:hidden path="productIds"/>
                            <%--<div class="form-group">--%>
                                <%--<spring:bind path="bondCompany.productIds">--%>
                                <%--<appfuse:label key="bondCompany.chooseProduct" path="bondCompany.productIds" styleClass="control-label col-sm-1"/>--%>
                                <%--<div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">--%>
                                    <%--</spring:bind>--%>
                                    <%--<form:select cssClass="form-control" path="productIds" id="productIds"  maxlength="255"  multiple="multiple">--%>
                                        <%--<form:options items="${availableProduct}" itemValue="value" itemLabel="label"/>--%>
                                            <%--</form:select>--%>
                                    <%--<form:errors path="productIds" cssClass="help-block"/>--%>
                                <%--</div>--%>
                                <%--</div>--%>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-primary" id="save" name="save" onclick="bCancel=false">
                                        <i class="icon-ok icon-white"></i> <fmt:message key="button.save"/>
                                    </button>
                                    <c:if test="${not empty bondCompany.id}">
                                        <button type="submit" class="btn btn-danger" id="delete" name="delete" onclick="bCancel=true;return confirmMessage(msgDelConfirm)">
                                            <i class="icon-trash icon-white"></i> <fmt:message key="button.delete"/>
                                        </button>
                                    </c:if>

                                    <button type="submit" class="btn btn-default" id="cancel" name="cancel" onclick="bCancel=true">
                                        <i class="icon-remove"></i> <fmt:message key="button.cancel"/>
                                    </button>
                                </div>

                            </form:form>
                        </div>
                    </div>
<c:if test="${not empty bondCompany.id}">
                    <div class="row">
                        <div class="col-sm-12">
                            <h3><fmt:message key="bondCompany.children"/> </h3>
                            <div id="bondChildrens"></div>
                            </div>
                    </div>
    <hr/>
    <div class="row">
        <div class="col-sm-12">
            <h3><fmt:message key="loanRequest.loanProductId"/> </h3>
            <form:form method="post" action="removeBondProducts">
                <input type="hidden" value="${bondCompany.id}" name="bondId"/>
            <display:table name="loanProductList" class="table table-bordered table-hover" requestURI="" id="loanProductList" export="false" pagesize="25">
                <display:column titleKey="button.select" sortable="false" style="width: 3%;">
                    <input type="checkbox" name="selectedIds" value="<c:out value="${loanProductList.id}"/>"/>
                </display:column>
                <display:column property="productName" sortable="true" href="loanProductform?from=list" media="html"
                                paramId="id" paramProperty="id" titleKey="loanProduct.productName"/>
                <display:column property="bank.name" sortable="true" titleKey="loanProduct.bank"/>
                <display:column sortable="true" titleKey="loanRequest.second">
                    <input type="checkbox" disabled="disabled" <c:if test="${loanProductList.second}">checked="checked"</c:if>/>
                </display:column>
                <display:column property="costRate" sortable="true" titleKey="loanProduct.costRate"/>
                <display:column property="coef" sortable="true" titleKey="loanProduct.coef"/>
                <display:column property="crLL" sortable="true" titleKey="loanProduct.crLL"/>
                <display:column property="crUL" sortable="true" titleKey="loanProduct.crUL"/>
                <display:column property="dpLL" sortable="true" titleKey="loanProduct.dpLL"/>
                <display:column property="dpUL" sortable="true" titleKey="loanProduct.dpUL"/>
                <display:column property="faLL" sortable="true" titleKey="loanProduct.faLL"/>
                <display:column property="faUL" sortable="true" titleKey="loanProduct.faUL"/>
                <display:column sortable="true" titleKey="loanProduct.loanYears">
                    <c:forEach var="year" items="${loanProductList.loanYears}" varStatus="status">
                        <fmt:message key="LOAN_YEARS_${year}"/><c:if test="${!status.last}">,</c:if>
                    </c:forEach>
                </display:column>
                <display:setProperty name="paging.banner.item_name"><fmt:message key="loanProductList.loanProduct"/></display:setProperty>
                <display:setProperty name="paging.banner.items_name"><fmt:message key="loanProductList.loanProducts"/></display:setProperty>

                <display:setProperty name="export.excel.filename"><fmt:message key="loanProductList.title"/>.xls</display:setProperty>
                <display:setProperty name="export.csv.filename"><fmt:message key="loanProductList.title"/>.csv</display:setProperty>
                <display:setProperty name="export.pdf.filename"><fmt:message key="loanProductList.title"/>.pdf</display:setProperty>
            </display:table>
                <!--<button type="submit" class="btn btn-danger" id="remove" name="remove" onclick="return checkBatchRemove();">
                    <i class="icon-ok icon-white"></i> <fmt:message key="bondCompany.action.removeProducts"/>
                </button>-->
            </form:form>
        </div>
    </div>
    <hr/>

                    <div class="row">
                        <div class="col-sm-12">
                            <h3><fmt:message key="loanRequest.dealer"/> </h3>
                            <display:table name="dealerList" class="table table-condensed table-striped table-hover" requestURI="" id="dealerList" export="false" pagesize="25">
                                <display:column property="name" sortable="true" href="dealerform?from=bond" media="html"
                                                paramId="dealerId" paramProperty="id" titleKey="dealer.name"/>
                                <display:column property="id" media="csv excel xml pdf" titleKey="dealer.id"/>
                                <display:column property="bond.name" sortable="true" titleKey="dealer.bondCompany"/>
                                <display:column property="adminUser.username" sortable="true" titleKey="admin.username"/>
                                <display:column property="adminUser.name" sortable="true" titleKey="dealer.contactPerson"/>
                                <display:column property="adminUser.phoneNumber" sortable="true" titleKey="dealer.contactNumber"/>
                                <display:column property="adminUser.email" sortable="true" titleKey="user.email"/>
                                <display:column property="province" sortable="true" titleKey="dealer.province"/>
                                <display:column property="city" sortable="true" titleKey="dealer.city"/>
                                <display:column property="mailAddress" sortable="true" titleKey="dealer.mailAddress"/>
                                <display:column sortable="true" titleKey="dealer.riskRank"><fmt:message key="RISK_RANK_${dealerList.riskRank}"/></display:column>

                                <display:setProperty name="paging.banner.item_name"><fmt:message key="dealerList.dealer"/></display:setProperty>
                                <display:setProperty name="paging.banner.items_name"><fmt:message key="dealerList.dealers"/></display:setProperty>

                                <display:setProperty name="export.excel.filename"><fmt:message key="dealerList.title"/>.xls</display:setProperty>
                                <display:setProperty name="export.csv.filename"><fmt:message key="dealerList.title"/>.csv</display:setProperty>
                                <display:setProperty name="export.pdf.filename"><fmt:message key="dealerList.title"/>.pdf</display:setProperty>
                            </display:table>
                            <div id="actions" class="btn-group margin">
                                <a href='<c:url value="/dealerform?bondId=${bondCompany.id}"/>' class="btn btn-primary">
                                    <i class="fa fa-plus"></i> <fmt:message key="button.add"/></a>
                            </div>
                        </div>
                    </div>
</c:if>
                </div>
            </div>
        </div>
    </div>

</section>

<c:set var="scripts" scope="request">
<v:javascript formName="bondCompany" cdata="false" dynamicJavascript="true" staticJavascript="false"/>
<script type="text/javascript" src="<c:url value='/scripts/validator.jsp'/>"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $( "#parent" ).combobox();
        if($("#parentValue").val()!=null){
            $("#parent").combobox('autocomplete', $('#parentValue').val(), $('#parentValueId').val());
        };
        function toJstree(list){
            var jsTreeList = [];
            list.forEach(function(i){
                jsTreeList.push({'id':i['id'],'text': i['name'], 'children':toJstree(i['children']) });

            })
            return jsTreeList;
        }
        if($("#bondCompanyId").val()){
            $.ajax('/bondCompanyform/children?bondId=${bondCompany.id}').done(function(data){
                if(data.length>0){

                    $('#bondChildrens').jstree({
                        "core" : {
                            "animation" : 0,
                            "check_callback" : true,
                            "themes" : { "stripes" : true },
                            'data' : toJstree(data),
                            'expand_selected_onload': true
                        },
                        "plugins" : [
                            "contextmenu", "dnd", "search",
                            "state", "types", "wholerow"
                        ]
                    });
                }
            })
        }


        $("input[type='text']:visible:enabled:first", document.forms['bondCompanyForm']).focus();
        //$('#productIds').multiSelect();

    });

    function checkBatchRemove() {
        var selectedIds = $("input[name='selectedIds']:checked").val();
        if (!selectedIds || selectedIds.length == 0) {
            alert(removeMessage);
            return false;
        } else {
            return true;
        }
    }

</script>
</c:set>